<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name='fade-right' mode="out-in">
        <XtxBreadItem  :key="topCategory.id" >{{topCategory.name}}</XtxBreadItem>
        </transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height:500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in topCategory.children" :key="i.id">
            <a href="javascript:;">
              <img v-lazy="i.picture" >
              <p>{{i.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
            <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item,o in arr" :key="o">
        <div class="head">

          <h3>- {{item.name}} -</h3>
          <p class="tag">{{item.desc}}</p>
          <XtxMore style="float:right" />

        </div>
        <div class="body">
          <GoodsItem v-for="(g,c) in item.goods" :key="c" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { findTopCategory } from '@/api/category'
import { findBanner } from '@/api/home'
import { computed, ref, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import GoodsItem from './components/goods-item.vue'
export default {
  components: { GoodsItem },
  name: 'TopCategory',
  setup () {
    // 轮播图
    const sliders = ref([])
    const arr = ref([])

    findBanner().then(data => {
      sliders.value = data.result
    })
    // 获取分类数据
    const route = useRoute()
    const store = useStore()
    const topCategory = computed(() => {
      const a = store.state.category.list.find(v => v.id === route.params.id) || {}
      return a
    })
    // 获取分类商品数据
    watch(() => route.params.id, async (a, b) => {
      console.log('改变了', a, b)
      // 🐒判断路由路径是否是有id且不是二级类目
      if (route.path === `/category/${route.params.id}`) {
        const { result } = await findTopCategory(route.params.id)
        arr.value = result.children
      }
      // console.log(result)
    }, { immediate: true })
    return { sliders, topCategory, arr }
  }
}
</script>
<style scoped lang="less">
.body{
  display: flex;
  justify-content: space-between;
}
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>
